package com.dyz.swordFingerOffer;

/**
 * @author: daiyizheng
 * @date: 2021/6/8 20:51
 * @description:
 */
public class GetNumberOfK56 {
    private int count = 0;
    public int GetNumberOfK(int [] array , int k) {
        if(array==null||array.length==0){
            return 0;
        }
        int left = 0;
        int right = array.length-1;

        while(left<=right){
            int mid = (left+right)/2;
            if(array[mid]==k){
                count++;
                int lm = mid;
                while(lm-1>=left && array[lm-1]==k){
                    count++;
                    lm--;
                }
                while(mid+1<=right && array[mid+1]==k){
                    count++;
                    mid++;
                }
                break;
            }else if(array[mid]>k){
                right= mid-1;
            }else{
                left=mid+1;
            }
        }
        return count;
    }
}
